package com.hzy.ums.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.hzy.ums.domain.bo.UmsLoginBody;
import com.hzy.ums.domain.vo.LoginMemberVo;
import com.hzy.ums.domain.vo.MemberVo;
import com.hzy.ums.request.RegisterRequest;
import com.hzy.ums.service.IMemberService;
import com.hzy.ums.service.TokenService;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Copyright (C), 2018-2023
 * FileName: MemberLoginCotroller
 * Description: 登录
 * Author:   zengxin(zengxin@kbostar.com)
 * Date:     2023/10/19 16:22
 */
@RestController
@RequestMapping
public class MemberController {

    @Autowired
    private IMemberService memberService;
    @Autowired
    private TokenService tokenService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @SaIgnore
    @PostMapping("/login")
    public R<LoginMemberVo> login(@Validated @RequestBody UmsLoginBody loginBody) {
        Map<String, Object> ajax = new HashMap<>();
        // 生成令牌
//        return R.ok(IAuthStrategy.login(loginBody));
        return R.ok();
    }

    /**
     * 用户注册
     */
    @RepeatSubmit()
    @PostMapping("/register")
    public R<Void> register(@Validated @RequestBody RegisterRequest registerRequest) {
//        memberService.register(user);
        memberService.validateVerifyCode(registerRequest.getUuid(), registerRequest.getCode());
//        IAuthStrategy.register(registerRequest);
        return R.ok();
    }

    @GetMapping("/validate/{phone}")
    public R<Void> validate(@PathVariable String phone){
        memberService.validate(phone);
        return R.ok();
    }

    /***
     * 新增会员登录记录
     * @param request
     * @return
     */
    @GetMapping("/member/info")
    public R<MemberVo> getMemberInfo(HttpServletRequest request){
        LoginMemberVo loginMember = tokenService.getLoginMember(request);
        Assert.notNull(loginMember, "获取用户ID异常");
        return R.ok(memberService.queryById(loginMember.getMemberId()));
    }


    @GetMapping("/h5/record/login")
    public void record(HttpServletRequest request) {
//        LoginMember loginMember = tokenService.getLoginMember(request);
//        if (loginMember != null){
//            service.insert(loginMember.getMemberId());
//        }
    }
}
